package com.unicampmaps;

import java.util.ArrayList;
import java.util.List;

import android.app.LauncherActivity.ListItem;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListItemAdapter extends BaseAdapter {
	private Context mContext;
	
	private List<Venue> venues;
	
	private DrawableManager dm; 
	
	// Gets the context so it can be used later
	public ListItemAdapter(Context c) {
		this.mContext = c;
		this.venues = new ArrayList<Venue>();
		
		this.dm = new DrawableManager();
	}
	
	public void addItem(Venue v) {
		this.venues.add(v);
	}

	// Total number of things contained within the adapter
	@Override
	public int getCount() {
		return this.venues.size();
	}

	// Require for structure, not really used in my code.
	@Override
	public Object getItem(int position) {
		return this.venues.get(position);
	}

	// Require for structure, not really used in my code. Can
	// be used to get the id of an item in the adapter for
	// manual control.
	@Override
	public long getItemId(int position) {
		return this.venues.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater li =  (LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View tempView = li.inflate(R.layout.list_item, null);
		
		ImageView icon = (ImageView) tempView.findViewById(R.id.list_icon);
		TextView textTop = (TextView) tempView.findViewById(R.id.list_label_top);
		TextView textBottom = (TextView) tempView.findViewById(R.id.list_label_bottom);
		
		Venue v = this.venues.get(position);
		
		textTop.setText(v.getName());
		
		textBottom.setText(v.getAbbr());


		icon.setVisibility(View.VISIBLE);
		icon.setImageResource(v.getImageResource());
		
		//Carrega assincronamente a imagem da internet, se houver
		if (!v.getImage().equalsIgnoreCase("")) {
			System.out.println(v.getImage());
			
			//ImageLoader loader = new ImageLoader(icon);
			//loader.execute(v.getImage());
			
			Drawable d = dm.fetchDrawable(v.getImage());
			
			icon.setImageDrawable(d);
		}

		return tempView;
	}
}
